import {Injectable} from '@angular/core';
import {HttpService} from '../http.service';
import {adSystemApiUrl, tuikeSystemUrl} from '../../../../config/api';

@Injectable()

export class RoleService {

    constructor(
        private httpService: HttpService
    ) {}

    /**
     * 获取角色列表
     * @param name
     * @param belong
     * @param orderField
     * @param orderSort
     * @param pageSize
     * @returns {Observable<R>}
     */
    getRoleList(name, belong, orderField, orderSort, page, pageSize) {
        const url = `${ adSystemApiUrl }/v1/admin/roles/list?name=${name}&belong=${belong}&orderField=${orderField}&orderSort=${orderSort}&page=${page}&pageSize=${pageSize}`;
        return this.httpService.httpGetAuth(url).map(data => data);
    }


    getPermissionList(page, pageSize) {
      const url = `${tuikeSystemUrl}/api/v1/admin/rbac/role_index?page=${page}&pageSize=${pageSize}`;
      return this.httpService.httpGetAuth(url).map(data => data);
    }

    /**
     * 获取角色详情
     * @param id
     * @returns {Observable<R>}
     */
    getRoleInfo(id) {
        const url = `${ adSystemApiUrl }/v1/admin/roles/${ id }/info`;
        return this.httpService.httpGetAuth(url).map(data => data);
    }

    /**
     * 角色添加
     * @param data
     * @returns {Observable<R>}
     */
    addRole(data) {
        const url = `${ adSystemApiUrl }/v1/admin/roles/add`;
        return this.httpService.httpPostAuth(url, data).map(data => data);
    }

    /**
     * 角色修改
     * @param data
     * @returns {Observable<R>}
     */
    editRole(data) {
        const url = `${ adSystemApiUrl}/v1/admin/roles/edit`;
        return this.httpService.httpPostAuth(url, data).map(data => data);
    }

    /**
     * 权限列表
     * @returns {Observable<R>}
     */
    getPermissionGroup() {
        const url = `${ adSystemApiUrl }/v1/admin/permissions/group_list`;
        return this.httpService.httpGetAuth(url).map(data => data);
    }

    /**
     * 角色权限
     * @param id
     * @returns {Observable<R>}
     */
    getRolePermission(id) {
        const url = `${ adSystemApiUrl }/v1/admin/permission_role/${ id }/get`;
        return this.httpService.httpGetAuth(url).map(data => data);
    }

    setRolePermission(permission_val, id) {
        const  url = `${ adSystemApiUrl }/v1/admin/permission_role/set`;
        return this.httpService.httpPostAuth(url, {'role_id': id, 'permission': permission_val}).map(data => data);
    }
}
